package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.ProductStockMapper;
import cn.wolfcode.crm.mapper.ReturnGoodsMapper;
import cn.wolfcode.crm.mapper.StockIncomeBillMapper;
import cn.wolfcode.crm.mapper.StockincomeMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IReturnGoodsService;
import cn.wolfcode.crm.util.PageResult;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;


@Service
public class ReturnServiceImpl implements IReturnGoodsService{

    @Autowired
    private ReturnGoodsMapper returnGoodsMapper;
    @Autowired
    private StockincomeMapper incomeMapper;
    @Autowired
    private StockIncomeBillMapper billMapper;
    @Autowired
    private ProductStockMapper stockMapper;

    public PageResult queryForList(QueryObject qo) {

        List<ReturnGoods> list = returnGoodsMapper.queryForList(qo);

        return new PageResult(list.size(),list);
    }

    //保存退货单
    public void returnGood(Long id) {

        //1.获取单据明细单
        StockIncomeBill bill = billMapper.selectByPrimaryKey(id);
        //2.通过billId去查询出单据
        Stockincome income = incomeMapper.selectByBillId(bill.getBillId());

        //1.先判断是否是入库状态
        if(income.getState() == Stockincome.STATE_NOMAL){//未入库状态，就删除明细单，减少单据数量
            billMapper.deleteByBillId(id);
            BigDecimal number = BigDecimal.ZERO;
            //减少单据数量
            number = income.getNumber().subtract(bill.getNumber());
            income.setNumber(number);
            income.setAmountPrice(income.getNumber().multiply(income.getCostPrice()).setScale(2, RoundingMode.HALF_UP));
        }else{ //已经入库的，去看某个仓库的某个商品有没有库存
            ProductStock stock = stockMapper.selectStockByProductSnAndDepotId(bill.getProduct().getProductName(),income.getDepot().getId());
            if(stock == null){
                throw new RuntimeException("当前状态还是未入库状态");
            }else { //有这个库存。
                BigDecimal number = stock.getNumber();
                BigDecimal amountPrice = stock.getAmountPrice();
                number = number.subtract(bill.getNumber());
                stock.setNumber(number);
                amountPrice = amountPrice.subtract(bill.getAmountPrice());
                stock.setAmountPrice(stock.getNumber().multiply(stock.getCostPrice()));
                System.out.println(amountPrice);
                //更新库存数量
                stockMapper.updateProductStockNumber(stock);
            }
        }
        Product pro = new Product();
        pro.setId(bill.getProduct().getId());
        ReturnGoods good = new ReturnGoods();
        good.setSn(income.getSn());
        good.setSupplier(income.getSupplier());
        good.setCostPrice(bill.getCostPrice());
        good.setProduct(pro);
        good.setNumber(bill.getNumber());
        good.setAmountPrice(bill.getAmountPrice());
        good.setReceipts(income.getReceipts());
        good.setInputTime(new Date());
        good.setProduct(bill.getProduct());


        returnGoodsMapper.insert(good);

//        //1.查询入库单
//        Stockincome stockincome = incomeMapper.getStockBySn(good.getSn());
//        if (stockincome.getState() == stockincome.STATE_NOMAL){ //如果是未入库状态，应该在单据中删除。
//
//        }
//        BigDecimal incomeNumber = stockincome.getNumber();
//        BigDecimal goodNumber = good.getNumber();
//        if(incomeNumber.compareTo(goodNumber)<0){
//            throw new RuntimeException("退货的数量大于入库的数量,请重新选择退货");
//        }else if(incomeNumber.compareTo(goodNumber)==0){
//            incomeMapper.deleteBySn(good.getSn());
//
//        }else {
//            stockincome.setNumber(incomeNumber.subtract(goodNumber));
//        }
//
//        returnGoodsMapper.insert(good);
//
//
//        System.out.println(good);
    }
}
